package com.tuanzi.loan.web.bean.statistics.advertise;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.statistics.LoanAdsChannelDetail;
import com.tuanzi.loan.business.service.statistics.LoanAdsChannelDetailService;
import com.tuanzi.loan.business.service.statistics.LoanAdvertiseTransFormationService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.advertise.AdvertiseTransStatRequest;
import com.tuanzi.loan.vo.response.advertise.AdvertiseTransStatResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.AdvertiseTransStatDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.ADS_PLATFORM_TYPE;

/**
 * 广告回调统计
 *
 * created by chuIllusions_tan 20180727
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class AdvertiseTransStatBean extends JSFListingBean<AdvertiseTransStatRequest, AdvertiseTransStatResponse>{


    @Autowired
    private LoanAdvertiseTransFormationService advertiseTransStatService;
    @Autowired
    private LoanAdsChannelDetailService loanAdsChannelDetailService;

    private List<LoanAdsChannelDetail> channelLists;

    private StreamedContent xlsFile;

    private Long sum = 0L;

    public StreamedContent downloadExcel() throws UnsupportedEncodingException {
        InputStream input = new ByteArrayInputStream(advertiseTransStatService.getExcel($).getBytes("GBK"));
        String fileName = System.currentTimeMillis() + ".csv";
        xlsFile = new DefaultStreamedContent(input, "application/vnd.ms-excel;charset=gb2312", fileName);
        return xlsFile;
    }

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new AdvertiseTransStatRequest();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 初始化默认当前日期
        Date date = null;
        try {
            date = sdf.parse(sdf.format(new Date()));
            $.setStartDate(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        lazyDataModel = new AdvertiseTransStatDataModel($);
        initChannelLists();
    }
    protected void initChannelLists(){
        channelLists =  loanAdsChannelDetailService.findAllChannel();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(ADS_PLATFORM_TYPE);
    }
    public void getCountSum(){
        sum = 0L;
        sum = advertiseTransStatService.getCountSum();
    }
}
